{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# data prep toolbox "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic query of a data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#input a dataframe, check basic informations of a dataframe\n",
    "#input: dataframe\n",
    "def basicinfo(df):\n",
    "    info = df.info()\n",
    "    nullcount = df.isnull().sum()\n",
    "    numunique = df.nunique()\n",
    "    print(info)\n",
    "    print(\"========== then check null values =======\")\n",
    "    print(nullcount)\n",
    "    print(\"========== then check unique values for each column =======\")\n",
    "    print(numunique)\n",
    "    return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check distribution of a categorical data\n",
    "# input: data, categorical colname\n",
    "def categorydist(df,colname):\n",
    "    import seaborn as sns\n",
    "    distvalues = df[colname].value_counts()\n",
    "    print(distvalues)\n",
    "    ax = sns.countplot(x=colname,data=df)\n",
    "    return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check distribution of a numerical data\n",
    "# input: data, number colname\n",
    "def numberdist(df,colname):\n",
    "    import seaborn as sns\n",
    "    distvalues = df[colname].describe()\n",
    "    print(distvalues)\n",
    "    ax = sns.distplot(df[colname].fillna(0))\n",
    "    return"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model Evaluation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Machine Learning Result Evalutaion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#evaluate classification result\n",
    "# input: y_test, y_predict\n",
    "def EvaluateModel(clf, X_test, y_test):\n",
    "    from sklearn.metrics import classification_report\n",
    "    from sklearn.metrics import precision_recall_curve\n",
    "    from sklearn.metrics import roc_curve, auc\n",
    "    \n",
    "    import matplotlib.pyplot as plt\n",
    "    import seaborn as sns\n",
    "    print(classification_report(y_test, y_predict, target_names=[\"not 1\", \"1\"]))\n",
    "    precision, recall, thresholds = precision_recall_curve(y_test, y_predict)\n",
    "    closest_zero = np.argmin(np.abs(thresholds))\n",
    "    closest_zero_p = precision[closest_zero]\n",
    "    closest_zero_r = recall[closest_zero]\n",
    "    plt.figure()\n",
    "    plt.xlim([0.0, 1.01])\n",
    "    plt.ylim([0.0, 1.01])\n",
    "    plt.plot(precision, recall, label='Precision-Recall Curve');\n",
    "    plt.plot(closest_zero_p, closest_zero_r, 'o', markersize = 12, fillstyle = 'none', c='r', mew=3);\n",
    "    plt.xlabel('Precision', fontsize=16);\n",
    "    plt.ylabel('Recall', fontsize=16);\n",
    "    ax = plt.gca()\n",
    "    ax.set_aspect('equal');\n",
    "    plt.show()\n",
    "    \n",
    "    y_prob = clf.predict_proba(X_test)[:,1]\n",
    "    fpr_lr, tpr_lr, _ = roc_curve(y_test, y_prob)\n",
    "    roc_auc_lr = auc(fpr_lr, tpr_lr)\n",
    "\n",
    "\n",
    "    plt.figure()\n",
    "    plt.xlim([-0.01, 1.00])\n",
    "    plt.ylim([-0.01, 1.01])\n",
    "    plt.plot(fpr_lr, tpr_lr, lw=3, label='LogRegr ROC curve (area = {:0.2f})'.format(roc_auc_lr))\n",
    "    plt.xlabel('False Positive Rate', fontsize=16)\n",
    "    plt.ylabel('True Positive Rate', fontsize=16)\n",
    "    plt.title('ROC curve (1-of-10 digits classifier)', fontsize=16)\n",
    "    plt.legend(loc='lower right', fontsize=13)\n",
    "    plt.plot([0, 1], [0, 1], color='navy', lw=3, linestyle='--')\n",
    "    ax = plt.gca()\n",
    "    ax.set_aspect('equal');\n",
    "    plt.show()"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
